﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Data.Objects.SqlClient;
using CCF20Model.ModelBase;
using System.Data.Objects;
namespace CCF20Model
{
    
    public class CustomizeModel : ModelBaseClass
    {
        
        public CustomizeModel(CCF20Entities ccf20Ent):base(ccf20Ent)
        {
            
        }
        public CustomizeTypeCollection<VehicleType> GetVehicleType()
        {

            if (ZICMonitorModel.IsJava)
            {
                var query = from m in ccf20Ent.VehicleTypes
                            orderby m.VehicleTypeID descending
                            select m;
                return new CustomizeTypeCollection<VehicleType>(query, ccf20Ent);
            }
            else
            {
                var query = from m in ccf20Ent.VehicleTypes
                            where m.Manufacture.Equals("FAW-VW")
                            orderby m.VehicleTypeID descending
                            select m;
                return new CustomizeTypeCollection<VehicleType>(query, ccf20Ent);
            }

        }
        
        public CustomizeTypeCollection<CustomizedTroubleCode> GetCustomizedTroubleCode()
        {
            var query = from m in ccf20Ent.CustomizedTroubleCodes
                        orderby m.CustomizedTroubleCodeID descending
                        select m;
            ccf20Ent.Refresh(RefreshMode.StoreWins, query);
            return new CustomizeTypeCollection<CustomizedTroubleCode>(query, ccf20Ent);
        }

        public CustomizeTypeCollection<WiseLinkIDS> GetWiseLinkIDS()
        {
            var query = from m in ccf20Ent.WiseLinkIDS
                        orderby m.ID descending
                        select m;
            ccf20Ent.Refresh(RefreshMode.StoreWins, query);
            return new CustomizeTypeCollection<WiseLinkIDS>(query, ccf20Ent);
        }

        public CustomizeTypeCollection<MaintenanceType> GetMaintenanceType()
        {
            if (ZICMonitorModel.IsJava)
            {
                var query = from m in ccf20Ent.MaintenanceTypes
                            orderby m.VehicleTypeID, m.Name, m.Priority
                            select m;
                RefreshQuery(query);
                return new CustomizeTypeCollection<MaintenanceType>(query, ccf20Ent);
            }
            else
            {
                var query = from m in ccf20Ent.MaintenanceTypes
                            where m.VehicleType.Manufacture.Equals("FAW-VW")
                            orderby m.VehicleTypeID, m.Name, m.Priority
                            select m;
                RefreshQuery(query);
                return new CustomizeTypeCollection<MaintenanceType>(query, ccf20Ent);
            }

             
        }
        public CustomizeTypeCollection<SMSTemplate> GetSMSTemplate(bool isUrgentIncluded = false)
        {
            //为防止误发事故短信，在客服模块里屏蔽事故短信模板
            if (isUrgentIncluded)
            {
                var query = from m in ccf20Ent.SMSTemplates
                            orderby m.SMSTitle descending
                            select m;
                RefreshQuery(query);
                return new CustomizeTypeCollection<SMSTemplate>(query, ccf20Ent);
            }
            else
            {
                var query = from m in ccf20Ent.SMSTemplates
                            where !m.SMSType.Equals("事故")
                            orderby m.SMSTitle descending
                            select m;
                RefreshQuery(query);
                return new CustomizeTypeCollection<SMSTemplate>(query, ccf20Ent);
            }
        }
        public CustomizeTypeCollection<SMSHistory> GetSMSHistory()
        {
            var query = from m in ccf20Ent.SMSHistories
                        orderby m.QueueTime descending
                        select m;
            return new CustomizeTypeCollection<SMSHistory>(query, ccf20Ent);
        }
        public CustomizeTypeCollection<RemindList> GetRemindList()
        {
            var query = from m in ccf20Ent.RemindLists
                        orderby m.RemindListID descending
                        select m;
            return new CustomizeTypeCollection<RemindList>(query, ccf20Ent);
        }
        public CustomizeTypeCollection<TroubledDevice> GetTroubledDevice()
        {
            var query = from m in ccf20Ent.TroubledDevices
                        orderby m.TroubleDeviceID descending
                        select m;
            return new CustomizeTypeCollection<TroubledDevice>(query, ccf20Ent);
        }
        public CustomizeTypeCollection<ManagerReportReceiver> GetManagerReportReceiver()
        {
            var query = from m in ccf20Ent.ManagerReportReceivers
                        orderby m.Name descending
                        select m;
            return new CustomizeTypeCollection<ManagerReportReceiver>(query, ccf20Ent);
        }
        public int GetMaxPriorityIDByMaintenanceType(int anchiID)
        {
            short? temp = ccf20Ent.MaintenanceTypes.Where(m => m.AnchiID == anchiID).Max(m => m.Priority);
            if (temp == null)
            {
                return 1;
            }
            else
            {
                return (int)temp;
            }
        }
        public IEnumerable<VehicleType> MyVehicleTypes()
        {
            if (ZICMonitorModel.IsJava)
            {
                return from v in ccf20Ent.VehicleTypes
                       select v;
            }
            else
            {
                return from v in ccf20Ent.VehicleTypes
                                 where v.Manufacture.Equals("FAW-VW")
                                 select v;
            }

        }
        public void RemoveDirectCode(string code, short? version, int? vehicleTypeId)
        {
            var closedFaults = ccf20Ent.Faults.Where(f => f.IsActive &&
                                                        f.IsConfirmed &&
                                                        f.Code == code &&
                                                        f.CodeVersion == version &&
                                                        f.Vehicle.VehicleTypeID == vehicleTypeId);
            foreach (Fault fault in closedFaults)
            {
                LoggerHelper.LogCCF20(string.Format("Undo Deleted DirectPass Fault Code = {0} Previous CloseDate={1}", fault.Code, fault.CloseDate));
                fault.IsConfirmed = false;
            }
            ccf20Ent.SaveChanges();
        }
        
        public void RemoveCustomizedTroubleCode(string code, short? version,int? vehicleTypeId)
        {
            //var closedFaults = ccf20Ent.Faults.Where(f => !f.IsActive &&
            //                                            f.CloseReason == "人工排除" &&
            //                                            f.PreviousCloseReason != null &&
            //                                            SqlFunctions.DateDiff("day", SqlFunctions.GetDate(), f.CloseDate) <= 1 &&
            //                                            f.Code == code &&
            //                                            f.CodeVersion == version &&
            //                                            f.Vehicle.VehicleTypeID == vehicleTypeId).OrderByDescending(f => f.HappenDate);
            var closedFaults = from f in ccf20Ent.Faults
                               where !f.IsActive && f.CloseReason == "人工排除" &&
                                        SqlFunctions.DateDiff("day", SqlFunctions.GetDate(), f.CloseDate) <= 1 &&
                                        f.Code == code &&
                                        f.CodeVersion == version &&
                                        f.Vehicle.VehicleTypeID == vehicleTypeId
                               group f by f.VehicleID into g
                               select g.OrderByDescending(r => r.HappenDate).FirstOrDefault();
                               
            foreach (Fault fault in closedFaults)
            {
                LoggerHelper.LogCCF20(string.Format("Undo Deleted Low Chance Fault Code = {0} Previous CloseDate={1}",fault.Code,fault.CloseDate));
                fault.IsActive = true;
                fault.CloseDate = null;
                fault.IsConfirmed = false;
                fault.CloseReason = fault.PreviousCloseReason;
            }
            ccf20Ent.SaveChanges();
        }
        public short SetMaintenancePriority(int? vehicleTypeID, string name)
        {
            MaintenanceType mt = ccf20Ent.MaintenanceTypes.Where(m => m.VehicleTypeID == vehicleTypeID && m.Name.Contains(name))
                                    .OrderByDescending(m=>m.Priority)
                                    .FirstOrDefault();
            if (mt == null)
            {
                return 1;
            }
            else
            {
                return ((short)(mt.Priority + 1));
            }

        }

    }
}
